package cn.dubito.android_linearing;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

/**
 * Created by apple on 18/2/9.
 * 菜单 Activity
 */

public class MenuActivity extends AppCompatActivity {

    private final String action = "cn.dubito.android_linearing.ACTION_START";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_layout);
        Button btn = findViewById(R.id.btn_shutdown);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                // 销毁Activity 和back键一样效果
//                finish();
                // 显式意图 -> 构造(当前的Activity,需要跳转的Activity)
//                Intent intent = new Intent(MenuActivity.this, SecondActivity.class);
                // 隐式意图 (定义的 action 名称,默认的category)
//                Intent intent = new Intent(action);
//                // category
//                intent.addCategory("cn.dubito.android_linearing.MY_CATEGORY");
                // 通过意图打开浏览器
//                Intent intent = new Intent(Intent.ACTION_VIEW);
//                intent.setData(Uri.parse("https://www.dubito.cn"));
//                Intent intent = new Intent(Intent.ACTION_DIAL);
//                intent.setData(Uri.parse("tel:10086"));
                Intent intent = new Intent(MenuActivity.this, SecondActivity.class);
                // 传输数据给指定的Activity
//                intent.putExtra("extra_data","Hello SecondActivity");
//                startActivity(intent);
                startActivityForResult(intent, 1);
            }
        });
    }

    /**
     * 上个Activity销毁时返回的数据
     *
     * @param requestCode 请求码
     * @param resultCode 返回码
     * @param data 结果数据
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String returnData = data.getStringExtra("data_return");
                    Toast.makeText(MenuActivity.this, returnData, Toast.LENGTH_SHORT).show();
                    Log.d("MenuActivity", returnData);
                }
                break;
            default:
        }
    }

    /**
     * 创建菜单
     *
     * @param featureId
     * @param menu
     * @return
     */
    @Override
    public boolean onCreatePanelMenu(int featureId, Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    /**
     * 操作菜单
     *
     * @param item 当前点击的item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add_item:
                Toast.makeText(MenuActivity.this, item.getTitle().toString(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.remove_item:
                Toast.makeText(MenuActivity.this, item.getTitle().toString(), Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;
    }
}
